const db = require("../db/index")

exports.getAricdeInfo = (req,res)=>{
const sql = 'select * from ev_aricde_cate'
db.query(sql,req,(err,results)=>{
  if (err) {
    return res.cc(err)
  }
  console.log(req.body,123456);
  // const test=[]
 attrValueList=[
//    [
//    '巅峰(大贸)',
//    '纽顿',
//    'Adirondack',
//    '欧恩培',
//    '纽翠斯(海淘)',
//    '渴望(大贸)',
//    '松狮',]
//  ,
 {
  attrList:
    ["全犬",
   '成犬',
   '幼犬',
   '老年犬',
   '全猫'],
 },
 {
  attrList:["加拿大",
  '新西兰',
  '美国',
  '意大利',
  '瑞士',
  '比利时',],

 },
 {
 attrList:[
  "全体型",
  '小犬型',
  '中大型犬',
  '大型犬',
  '中型犬',
  '迷你型犬',
]},
{
attrList:[
  "通用型",
  '全犬型',
]},
{attrList:[
  '小包(1.5-5kg)',
  '中包(5.1-10kg)',
  '大包(10.1kg以上)',
  '超小包(1.5kg以下)',
 ],
},

{attrList:[
  "鸡肉",
  '混合口味',
  '鱼肉',
  '鸭肉',
  '牛肉',
  '其他', 
]}
]


  let attrsList = results.map((item,index) => {
    return Object.assign({},{'attrId':item.attrId,'attrName':item.attrName,'attrValueList':attrValueList[index]})
})
 for (let i = 0; i < attrsList.length; i++) {
  if( attrsList[i].attrName == null)  
  {  
      attrsList.splice(i,1);
      i= i-1;
  } 
 }
//  console.log(attrsList);
let list = results.map((item,index) => {
  return Object.assign({},{
  'id':item.id,
  'defaultImg':item.defaultImg,
  'title':item.title,
  'price':item.price,
  'hotScore':item.hotScore,
  'category1Id':req.body.category1Id,
  'categoryName':item.categoryName,
  'category2Id':req.body.category2Id,
  'category2Name':item.category2Name,
  'category3Id':req.body.category3Id,
  'category3Name':item.category3Name,
})
})
let trademarkList = results.map((item,index) => {
  return Object.assign({},{'tmId':item.tmId,'tmName':item.tmName})
})
for (let i = 0; i < trademarkList.length; i++) {
  if( trademarkList[i].tmName == null)  
  {  
      trademarkList.splice(i,1);
      i= i-1;
  } 
 }
// let pageNo = 1;
if (req.body.order=='2:desc') {
  list.sort(function (a, b) {
    return b.price - a.price;
})
}
if (req.body.order=='2:asc') {
  list.sort(function (a, b) {
    return a.price - b.price;
})
}
if (req.body.keyword) {
 list = list.filter(item=>{
    return item.title.indexOf(req.body.keyword)!=-1;
  })
}
if (req.body.trademark) {
 list = list.filter(item=>{
    return item.title.indexOf(req.body.trademark)!=-1;
  })
  if (req.body.attrValue) {
    list = list.filter(item=>{
      return item.title.indexOf(req.body.attrValue)!=-1;
    })
  }      
}
if (req.body.categoryName) {
  list = list.filter(item=>{
    if (item.categoryName==null) {
      item.categoryName=''
    }
     return item.categoryName.indexOf(req.body.categoryName)!=-1;
   })
 }
if (req.body.attrValue) {
  list = list.filter(item=>{
    return item.title.indexOf(req.body.attrValue)!=-1;
  })}
// console.log(list);
 let = goodsList = list.slice((req.body.pageNo-1)*10,req.body.pageNo*10) 
  res.send({
    status: 0,
    message: '获取用户基本信息成功！',
    data: {
      attrsList,
      goodsList,
      trademarkList,
      total:list.length,
      pageNo:req.body.pageNo,
      pageSize:10,
      totalPages: Math.ceil(list.length / 10)
    }
  })
  // res.forEach(item => {
  //  let name = {name:item.name}
  // });
 
})
}